package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@RequiredArgsConstructor
@Component
public class ScheduleTasks {
    private final LeaseAgreementService leaseAgreementService;
    /*@Scheduled(cron ="* * * * * *")//倒过来的--年月日时分秒
    public void test()
    {
        System.out.println(new Date());
    }*/

    @Scheduled(cron = "0 0 0 * * *")//每天执行
    public void checkLeaseStatus()
    {
        LambdaUpdateWrapper<LeaseAgreement> w=new LambdaUpdateWrapper<>();
        w.le(LeaseAgreement::getLeaseEndDate, new Date())
                .in(LeaseAgreement::getStatus, List.of(LeaseStatus.SIGNED,LeaseStatus.WITHDRAWN))
                .set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED);
        leaseAgreementService.update(w);
    }
}
